package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.g2room.myuall.grap.univ.po.ProvScore;
import com.g2room.myuall.grap.univ.to.ProvScoreTo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.qiniu.util.Json;

public class GrapEolScore implements Runnable {
	// 2017年分数线
	static String pageUrl = "http://data.api.gkcx.eol.cn/soudaxue/queryProvince.html?messtype=json&url_sign=queryprovince&province3=&year3=&size=50&luqutype3=&luqupici3=&schoolsort=&suiji=&callback=jQuery&_=1470649027111&page="; 
	static PreparedStatement  errPs = null;
	
	public static void main(String[] args) {
			GrapEolScore gue = new GrapEolScore();
//			gue.getList("");  // 单个地址
//			String body  = "{\"totalRecord\":{\"num\":\"1644\"},\"school\":[{\"province\":\"安徽\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"文科\",\"score\":\"200\"},{\"province\":\"辽宁\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"理科\",\"score\":\"180\"},{\"province\":\"辽宁\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"文科\",\"score\":\"180\"},{\"province\":\"河北\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"理科\",\"score\":\"200\"},{\"province\":\"河北\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"文科\",\"score\":\"200\"},{\"province\":\"重庆\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"理科\",\"score\":\"140\"},{\"province\":\"重庆\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"文科\",\"score\":\"140\"},{\"province\":\"西藏\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"文科\",\"score\":\"249\"},{\"province\":\"西藏\",\"year\":\"2017\",\"bath\":\"专科一批\",\"type\":\"理科\",\"score\":\"240\"},{\"province\":\"浙江\",\"year\":\"2017\",\"bath\":\"平行录取一段\",\"type\":\"\",\"score\":\"577\"},{\"province\":\"浙江\",\"year\":\"2017\",\"bath\":\"平行录取二段\",\"type\":\"\",\"score\":\"480\"},{\"province\":\"浙江\",\"year\":\"2017\",\"bath\":\"平行录取三段\",\"type\":\"\",\"score\":\"359\"}]}";
//			ProvScoreTo adminScoreTo = Json.decode(body, ProvScoreTo.class);
//			GrapEolScore ges = new GrapEolScore();
//			ges.insert2Db(adminScoreTo.getSchool());
			
			List<String> listUrls = getUrl();
			if(null == listUrls) return;
			for(String url : listUrls) {
				gue.getList(url);
			}
			
//		// 以下为抓取 167
//		for(int i =0; i<17; i++) {
//			try {
//				Thread.sleep(100);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			GrapEolScore gue = new GrapEolScore();
//			Thread t = new Thread(gue);
//			t.start();
//		}
	}
	

	@Override
	public void run() {
		List<String> listUrls = getUrl();
		if(null == listUrls) return;
		for(String url : listUrls) {
			getList(url);
		}
	}
	
	public static AtomicInteger page = new AtomicInteger(3);
	private static synchronized List<String> getUrl() {
		if(page.intValue() > 33) return null;
		List<String> listUrls = new ArrayList<String>();
		for(int i = page.intValue(); i< 4; i++) {
			listUrls.add(pageUrl+i);
		}
		page.addAndGet(10);
		return listUrls;
	}

	
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		System.out.println("start get page data!==" + url);
		String body = null;
		try {
			body = new GenericMyuall().getJsonContent(url);
		} catch (IOException e1) {
			e1.printStackTrace();
			insert2ErrDb(url, 1);
			return;
		}
		if(null == body) return;
		ProvScoreTo adminScoreTo = Json.decode(body, ProvScoreTo.class);
		insert2Db(adminScoreTo.getSchool());
	}
	static List<String> scoreExist = new  ArrayList<String>();
	public void insert2Db(List<ProvScore> ass) {
		try {
			PreparedStatement univPs = DbUtil.getConn().prepareStatement("INSERT INTO `prov_score` ("
					+ "province_name,year,type,batch_name,score) "
					+ "values(?,?,?,?,?)");
			for(ProvScore as : ass) {
				if(scoreExist.contains(as.getBath() + as.getProvince() + as.getScore() + as.getType() + as.getYear())) continue;
				
				scoreExist.add(as.getBath() + as.getProvince() + as.getScore() + as.getType() + as.getYear());
				univPs.setString(1, as.getProvince());
				univPs.setString(2, as.getYear());
				univPs.setString(3, as.getType());
				univPs.setString(4, as.getBath());
				univPs.setInt(5, as.getScore());
				univPs.addBatch();
			}
			univPs.executeBatch();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	int sqlErrCount = 0;
	public void insert2ErrDb(String url, int type) {
		try {
			if(sqlErrCount == 0) {
				errPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_err` (`url`, `type`) "
					+ "values(?,?)");
			}
			errPs.setString(1, url);
			errPs.setInt(2, type);
			errPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}